local tongwei = fk.CreateSkill {
  name = "lingling__tongwei",
}

Fk:loadTranslationTable{
  ["lingling__tongwei"] = "统围",
  [":lingling__tongwei"] = "出牌阶段限两次，你可以令一名本回合未选择过的其他角色获得一张【闪】或【无懈可击】，直到你下回合开始，"..
  "当其下一次使用同名牌后，你获得其一至两张牌。",

  ["#lingling__tongwei"] = "统围：令一名角色获得一张【闪】或【无懈可击】，其下次使用同名牌后你获得其一至两张牌",
  ["@@lingling__tongwei_jink"] = "统围 闪",
  ["@@lingling__tongwei_nullification"] = "统围 无懈可击",
  ["#lingling__tongwei-prey"] = "统围：获得 %dest 一至两张牌",
}

tongwei:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#lingling__tongwei",
  interaction = function(self, player)
    local cards = table.connect(Fk:currentRoom().draw_pile, Fk:currentRoom().discard_pile)
    local choices = {}
    for _, name in ipairs({"jink", "nullification"}) do
      if table.find(cards, function (id)
        return Fk:getCardById(id).trueName == name
      end) then
        table.insert(choices, name)
      end
    end
    if #choices == 0 then return end
    return UI.CardNameBox { choices = choices, all_choices = {"jink", "nullification"} }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(tongwei.name, Player.HistoryPhase) < 2 and
      (table.find(Fk:currentRoom().draw_pile, function (id)
        return table.contains({"jink", "nullification"}, Fk:getCardById(id).trueName)
      end) or table.find(Fk:currentRoom().discard_pile, function (id)
        return table.contains({"jink", "nullification"}, Fk:getCardById(id).trueName)
      end))
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and
      not table.contains(player:getTableMark("lingling__tongwei-phase"), to_select.id)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:addTableMark(player, "lingling__tongwei-phase", target.id)
    local name = self.interaction.data
    local card = room:getCardsFromPileByRule(name, 1, "allPiles")
    if #card == 0 then return end
    room:moveCardTo(card, Card.PlayerHand, target, fk.ReasonJustMove, tongwei.name, nil, true, player)
    if target.dead then return end
    room:addPlayerMark(target, "@@lingling__tongwei_"..name, 1)
    if not player.dead then
      room:addTableMark(player, tongwei.name, {target.id, name})
    end
  end,
})

tongwei:addEffect(fk.CardUseFinished, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return table.find(player:getTableMark(tongwei.name), function (dat)
      return dat[1] == target.id and dat[2] == data.card.trueName
    end) and not target.dead
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {target}})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "@@lingling__tongwei_"..data.card.trueName, 0)
    local mark = player:getTableMark(tongwei.name)
    for i = #mark, 1, -1 do
      if mark[i][1] == target.id and mark[i][2] == data.card.trueName then
        table.remove(mark, i)
      end
    end
    room:setPlayerMark(player, tongwei.name, mark)
    if not target:isNude() then
      local cards = room:askToChooseCards(player, {
        target = target,
        min = 1,
        max = 2,
        flag = "he",
        skill_name = tongwei.name,
        prompt = "#lingling__tongwei-prey::"..target.id,
      })
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, tongwei.name, nil, false, player)
    end
  end,
})

tongwei:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark(tongwei.name) ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark(tongwei.name)
    room:setPlayerMark(player, tongwei.name, 0)
    for _, info in ipairs(mark) do
      local p = room:getPlayerById(info[1])
      if not p.dead then
        room:removePlayerMark(p, "@@lingling__tongwei_"..info[2], 1)
      end
    end
  end,
})

tongwei:addLoseEffect(function (self, player, is_death)
  if is_death then
    local room = player.room
    local mark = player:getTableMark(tongwei.name)
    room:setPlayerMark(player, tongwei.name, 0)
    for _, info in ipairs(mark) do
      local p = room:getPlayerById(info[1])
      room:removePlayerMark(p, "@@lingling__tongwei_"..info[2], 1)
    end
  end
end)

return tongwei
